/**
 * ReportGear(2011)
 */
package com.reportgear.report.view.grid.editor.impl;

import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;

import com.reportgear.core.swing.layout.LayoutHelper;
import com.reportgear.core.swing.pane.base.EditorPane;
import com.reportgear.report.model.cell.value.salary.SalaryFormulaObject;
import com.reportgear.report.model.cell.value.salary.SalaryItemObject;
import com.reportgear.report.model.cell.value.support.CommonSalaryItemObject;
import com.reportgear.swing.controls.JTreeComboBox;

/**
 * 工资定制编辑面板
 * 
 * @version 1.0 2011-3-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class SalaryFormulaObjectEditPane extends EditorPane<SalaryFormulaObject> {
	/**
	 * UID
	 */
	private static final long serialVersionUID = 7691163881244333612L;
	private JTextField txtCaption = new JTextField();
	private JTextArea txtFormula = new JTextArea();
	private SalaryItemPane salaryItemPane = new SalaryItemPane();
	private SalaryFormulaObject sfo;

	public SalaryFormulaObjectEditPane() {
		this.setBorder(BorderFactory.createEtchedBorder());
		this.setLayout(new GridBagLayout());

		this.add(new JLabel("标题", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 0, 1, 1));
		this.add(txtCaption, LayoutHelper.newGridBagConstraints(1, 0, 1, 1));
		this.add(new JLabel("公式", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 1, 1, 1));
		this.txtFormula.setRows(4);
		this.txtFormula.setLineWrap(true);
		this.txtFormula.setBorder(BorderFactory.createEtchedBorder());
		this.add(txtFormula, LayoutHelper.newGridBagConstraints(1, 1, 1, 1));
		this.add(new JLabel(), LayoutHelper.newGridBagConstraints(0, 2, 1, 1));

		JPanel btnPane = new JPanel();
		btnPane.setLayout(new GridLayout(1, 5));
		ActionListener btnListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				Object src = evt.getSource();
				if (src instanceof JButton) {
					JButton btn = (JButton) src;

					applyText(btn.getText());
				}

			}
		};
		
		JButton btnOpr;
		btnOpr = new JButton("+");
		btnOpr.addActionListener(btnListener);
		btnPane.add(btnOpr);
		btnOpr = new JButton("-");
		btnOpr.addActionListener(btnListener);
		btnPane.add(btnOpr);
		btnOpr = new JButton("*");
		btnOpr.addActionListener(btnListener);
		btnPane.add(btnOpr);
		btnOpr = new JButton("/");
		btnOpr.addActionListener(btnListener);
		btnPane.add(btnOpr);
		btnOpr = new JButton("清空");
		btnOpr.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				txtFormula.setText("");
			}
		});
		btnPane.add(btnOpr);
		this.add(btnPane, LayoutHelper.newGridBagConstraints(1, 2, 1, 1));

		this.add(this.salaryItemPane, LayoutHelper.newGridBagConstraints(0, 3, 2, 1));

		this.salaryItemPane.addItemChangeListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JTreeComboBox c = (JTreeComboBox) e.getSource();
				DefaultMutableTreeNode selectNode = c.getSelectedTreeNode();
				CommonSalaryItemObject co = (CommonSalaryItemObject) selectNode.getUserObject();
				applyText(co.getName() + "[" + co.getId() + "]");
			}
		});
	}

	private void applyText(String text) {
		if ((text == null) || (text.length() <= 0)) {
			return;
		}

		this.txtFormula.insert(text, this.txtFormula.getCaretPosition());
		this.txtFormula.getText();
		if (text.indexOf("()") != -1) {
			this.txtFormula.setCaretPosition(this.txtFormula.getCaretPosition() - 1);
		}

		this.txtFormula.requestFocus();
	}

	@Override
	public void populate(SalaryFormulaObject value) {
		this.sfo = value;
		this.salaryItemPane.populate(new SalaryItemObject());

		this.txtCaption.setText(value.getCaption());
		this.txtFormula.setText(value.getFormula());
	}

	@Override
	public int showDialog(Component owner) {
		return this.showDialog(SwingUtilities.getWindowAncestor(owner), "薪酬项目计算", 400, 280);
	}

	@Override
	public SalaryFormulaObject update() {
		this.sfo.setCaption(this.txtCaption.getText().trim());
		this.sfo.setFormula(this.txtFormula.getText().trim());

		return this.sfo;
	}
}
